﻿using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.OleDb;
using QLQTPM_ThuDuc_Group7.DTO;


namespace DesignLayOut_QLQTPM_Team7.Data
{
    public class DuLieuRepository
    {
        private string connectString;
        public DuLieuRepository()
        {
            connectString = ConfigurationSettings.AppSettings["ConnectionString"];
            string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            connectString = connectString.Replace("|DataDirectory|", path);
        }

        public bool ThemDuLieu(DuLieuDTO duLieu)
        {
            bool kq = false;
            try
            {
                OleDbConnection cnn = new OleDbConnection(connectString);
                OleDbCommand objCmd = new OleDbCommand("Insert into customers(madichvu, matendiadiem, sonha, maduong, maphuong, maquanhuyen, matinhthanh, kinhdo, vido, chuthich)"
                + "values (@madichvu, @matendiadiem, @sonha, @maduong, @maphuong, @maquanhuyen, @matinhthanh, @kinhdo, @vido, @chuthich)", cnn);

                objCmd.Parameters.Add("@madichvu", OleDbType.Integer).Value = duLieu.maDichVu;
                objCmd.Parameters.Add("@matendiadiem", OleDbType.Integer).Value = duLieu.maTenDiaDiem;
                objCmd.Parameters.Add("@sonha", OleDbType.VarWChar).Value = duLieu.soNha;
                objCmd.Parameters.Add("@maduong", OleDbType.Integer).Value = duLieu.maDuong;
                objCmd.Parameters.Add("@maphuong", OleDbType.Integer).Value = duLieu.maPhuong;
                objCmd.Parameters.Add("@maquanhuyen", OleDbType.Integer).Value = duLieu.maQuanHuyen;
                objCmd.Parameters.Add("@matinhthanh", OleDbType.Integer).Value = duLieu.maTinhThanh;
                objCmd.Parameters.Add("@kinhdo", OleDbType.Integer).Value = duLieu.kinhDo;
                objCmd.Parameters.Add("@vido", OleDbType.Integer).Value = duLieu.viDo;
                objCmd.Parameters.Add("@chuthich", OleDbType.VarWChar).Value = duLieu.chuThich;

                cnn.Open();
                int n = objCmd.ExecuteNonQuery();
                if (n == 1)
                    kq = true;
                cnn.Close();
                kq = true;
            }
            catch (Exception ex)
            {
                //Log4Net
                Console.WriteLine(ex.Message);
            }
            return kq;
        }

        public int ThemDuLieu(List<DuLieuDTO> lstDuLieu, ref int rowData)
        {
            rowData = 0;
            int dem = 0;//Đếm số lượng insert thành công
            try
            {
                OleDbConnection cnn = new OleDbConnection(connectString);
                cnn.Open();
                for (int i = 0; i < lstDuLieu.Count; i++)
                {
                    DuLieuDTO duLieu = lstDuLieu.ElementAt(i);
                    OleDbCommand objCmd = new OleDbCommand("Insert into dulieu(madichvu, matendiadiem, sonha, maduong, maphuong, maQuanHuyen, matinhthanh, kinhdo, vido, chuthich)"
                    + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", cnn);

                    objCmd.Parameters.Add("@madichvu", OleDbType.Integer).Value = duLieu.maDichVu;
                    objCmd.Parameters.Add("@matendiadiem", OleDbType.Integer).Value = duLieu.maTenDiaDiem;
                    objCmd.Parameters.Add("@sonha", OleDbType.VarWChar).Value = duLieu.soNha;
                    objCmd.Parameters.Add("@maduong", OleDbType.Integer).Value = duLieu.maDuong;
                    objCmd.Parameters.Add("@maphuong", OleDbType.Integer).Value = duLieu.maPhuong;
                    objCmd.Parameters.Add("@maQuanHuyen", OleDbType.Integer).Value = duLieu.maQuanHuyen;
                    objCmd.Parameters.Add("@matinhthanh", OleDbType.Integer).Value = duLieu.maTinhThanh;
                    objCmd.Parameters.Add("@kinhdo", OleDbType.Integer).Value = duLieu.kinhDo;
                    objCmd.Parameters.Add("@vido", OleDbType.Integer).Value = duLieu.viDo;
                    objCmd.Parameters.Add("@chuthich", OleDbType.VarWChar).Value = duLieu.chuThich;
                    int n = 0;
                    try
                    {
                        n = objCmd.ExecuteNonQuery();
                    }
                    catch (OleDbException oleEX)
                    {
                        //Log4Net
                        Console.WriteLine(oleEX.Message);
                    }
                    if (n == 1)
                        dem++;
                    rowData++;
                }
                cnn.Close();
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return dem;
        }


    }
}
